const express = require('express')
const app = express()
const port = 7878
const router = require('./router/index')
const cors = require('cors')
require('./mongodb/index')

// 跨域处理
app.use(cors())
// post请求 表单数据
app.use(express.urlencoded({ extended: true }))
// 静态资源共享
app.use(express.static('upload/user'))
app.use(express.static('upload/group'))
app.use(express.static('upload/space'))
app.use(express.static('upload/private_chat'))
app.use(express.static('upload/group_chat'))
app.use(express.static('public/img'))

app.use('/', router)

// http服务器
app.listen(port, () => {
    console.log(`🌍server running at http://127.0.0.1:${port}`)
})

/**======================================== socket.io ==================================== */
// 创建socket服务器
const http = require('http')
const socketIO = require('socket.io')
const server = http.createServer(app)
const socket_port = 24000
 
const io = socketIO(server,{
    cors: {
        origin: '*'
    }
})
require('./socket/index')(io)

server.listen(socket_port, () => {
    console.log("🛰️ socket_server running at http://127.0.0.1:24000");
})